const mongoose = require("mongoose");
const moviesModel = mongoose.model("movies");

//电影信息接口
module.exports = {

    //获取某一条的电影相关信息
    getOne: async _id => {
        return await moviesModel.findOne({ _id });
    },
    //删除某一条的电影相关信息
    removeMovie: async ({ _id }) => {
        return await moviesModel.deleteOne({ _id });
    },
    //修改电影信息
    update: async ({ _id, movies }) => {
        return await moviesModel.updateOne({ _id }, movies);
    },
    //添加电影
    add: async movies => {
        return await moviesModel.create(movies)
    },
    //添加电影图片
    addImg: async ({ _id, url }) => {
        return await moviesModel.updateOne({ _id }, {
            $push: {
                images: url
            }
        });
    },
    //添加海报
    addPoster: async ({ _id, url }) => {
        return await moviesModel.updateOne(
            { _id },
            { poster: url }
            )
    },
    //删除某一条的电影图片信息
    async removeImg({ _id, url }) {
        const movies = await this.getOne({ _id });//获取被删图片的电影信息
        movies.images.pull(url);
        return await movies.save();
    },
    getAll: async () => await moviesModel.find(),

}